package cn.itcast.service.impl;

import cn.itcast.dao.CountViewDao;
import cn.itcast.service.CountViewService;
import cn.itcast.utils.DateUtils;
import cn.itcast.vo.CountVo;
import cn.itcast.vo.Data_Plus;
import cn.itcast.vo.HotSetmeal;
import cn.itcast.vo.SumCount;
import com.alibaba.dubbo.config.annotation.Service;

import org.springframework.beans.factory.annotation.Autowired;



import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


@Service
public class CountViewServiceImpl implements CountViewService {

    @Autowired
    private CountViewDao countViewDao;
    @Override
    public List<CountVo> vipCount() {
        List<CountVo> list = countViewDao.vipCount();
        System.out.println(list);
        return list;
    }

    @Override
    public List<CountVo> setmealCount() {
        List<CountVo> list = countViewDao.setmealCount();
        return list;
    }

    @Override
    public List<CountVo> incomeCount() {
        List<CountVo> list = countViewDao.incomeCount();
        return list;
    }

    @Override
    public List<SumCount> orderCount() {
        List<SumCount> list = countViewDao.orderCount();
        return list;
    }

    @Override
    public Data_Plus findBusiness() {
       List<HotSetmeal> hotSetmeal= countViewDao.findsetmeal();
       double sum=0;
        for (HotSetmeal setmeal : hotSetmeal) {
            sum=sum+setmeal.getSetmeal_count();
        }
        System.out.println(sum);
        for (HotSetmeal setmeal : hotSetmeal) {
            setmeal.setProportion(setmeal.getSetmeal_count()/sum);
        }

        Data_Plus dataPlus = new Data_Plus();
        LocalDateTime today = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String s = today.format(formatter);
        //存入套餐内容
        dataPlus.setHotSetmeal(hotSetmeal);
        //设置日期
        dataPlus.setReportDate(s);
        //总会员数
        int memberSum=countViewDao.selectSum();
        dataPlus.setTotalMember(memberSum);
        //查询本日会员
        int todayMember=countViewDao.selectByDay(s);
        dataPlus.setTodayNewMember(todayMember);
        //查询本周会员
        Date date = new Date();
        Date firstDay1 = DateUtils.getFirstDayOfWeek(date);
        String firstDay = format.format(firstDay1);
        Date lastDay1 = DateUtils.getLastDayOfWeek(date);
        String  lastDay= format.format(lastDay1);

        System.out.println(lastDay);

        int weekMember=countViewDao.selectByWeek(firstDay,lastDay);
        dataPlus.setThisWeekNewMember(weekMember);
        //查询本月会员
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM");
        String month = today.format(formatter1);
        int monthMember=countViewDao.selectByMonth(month);
        dataPlus.setThisMonthNewMember(monthMember);
        //本日预约数
        int todayorder=countViewDao.todayorder(s);
        dataPlus.setTodayOrderNumber(todayorder);
        //本日到诊数
        int todayvisitor=countViewDao.todayvisitor(s,1);
        dataPlus.setTodayVisitsNumber(todayvisitor);
        //本周预约数
        int weekorder=countViewDao.weekorder(firstDay,lastDay);
        dataPlus.setThisWeekOrderNumber(weekorder);
        //本周到诊数
        int weekvisitor=countViewDao.weekvisitor(firstDay,lastDay,1);
        dataPlus.setThisWeekVisitsNumber(weekvisitor);
        //本月预约数
        int monthorder=countViewDao.monthorder(month);
        dataPlus.setThisMonthOrderNumber(monthorder);
        //本月到诊数
        int monthvisitor=countViewDao.monthvisitor(month,1);
        dataPlus.setThisMonthVisitsNumber(monthvisitor);

        //返回对象
        return dataPlus;
    }

}
